package sk.gtug.pivoplus.server.rest;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import sk.gtug.pivoplus.server.domain.twig.TwigEvaluationType;
import sk.gtug.pivoplus.server.domain.twig.TwigKrcmicka;
import sk.gtug.pivoplus.server.domain.twig.TwigKrcmickaEvaluation;
import sk.gtug.pivoplus.server.domain.twig.TwigPivar;
import sk.gtug.pivoplus.server.mock.Mocks;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.vercer.engine.persist.ObjectDatastore;
import com.vercer.engine.persist.annotation.AnnotationObjectDatastore;

/**
 * @author ladislav.gazo
 */
@Path("/pivo")
public class PivoResource {
	private ObjectDatastore dataStore = new AnnotationObjectDatastore();

	@GET
	@Path("/krcmy")
	@Produces({ MediaType.APPLICATION_JSON })
	public List<TwigKrcmicka> getPubs(@QueryParam("latitude") Double latitude,
			@QueryParam("longitude") Double longitude,
			@QueryParam("radius") Double radius) {
		Iterator<TwigKrcmicka> iterator = null;
		if (latitude == null || longitude == null || radius == null) {
			iterator = dataStore.find().type(TwigKrcmicka.class)
					.returnResultsNow();
		} else {
			iterator = dataStore
					.find()
					.type(TwigKrcmicka.class)
					.addFilter("latitude", FilterOperator.LESS_THAN_OR_EQUAL,
							latitude + radius)
					.addFilter("latitude",
							FilterOperator.GREATER_THAN_OR_EQUAL,
							latitude - radius)
					.addSort("latitude", SortDirection.ASCENDING)
					.addSort("longitude", SortDirection.ASCENDING)
					.returnResultsNow();
		}

		List<TwigKrcmicka> list = new ArrayList<TwigKrcmicka>();
		while (iterator.hasNext()) {
			list.add(iterator.next());
		}
		return list;
	}

	@POST
	@Path("/krcma/{id}/{userId}/{evalTypeId}")
	public String evaluatePub(@PathParam("id") String id,
			@PathParam("evalTypeId") String evalTypeId,
			@PathParam("userId") String userId,
			@FormParam("value") Double value,
			@FormParam("comment") String comment) {
		TwigKrcmickaEvaluation eval = new TwigKrcmickaEvaluation();

		TwigKrcmicka krcmicka = dataStore.load(TwigKrcmicka.class, id);
		TwigEvaluationType type = dataStore.load(TwigEvaluationType.class,
				evalTypeId);
		TwigPivar pivar = dataStore.load(TwigPivar.class, userId);

		eval.setKrcmicka(krcmicka);
		eval.setType(type);
		eval.setPivar(pivar);
		eval.setValue(value);
		eval.setComment(comment);

		Key key = dataStore.store(eval);

		return key.toString();
	}

	@GET
	@Path("/krcma/{id}")
	@Produces({ MediaType.APPLICATION_JSON })
	public TwigKrcmicka getPub(@PathParam("id") String id) {
		return dataStore.load(TwigKrcmicka.class, id);
	}

	@GET
	@Path("/evaluation/{id}/{userId}")
	@Produces({ MediaType.APPLICATION_JSON })
	public List<TwigKrcmickaEvaluation> getEvaluation(
			@PathParam("id") String id, @PathParam("userId") String userId) {
		TwigKrcmicka pub = dataStore.load(TwigKrcmicka.class, id);
		TwigPivar beerer = dataStore.load(TwigPivar.class, userId);
		Iterator<TwigKrcmickaEvaluation> iterator = dataStore
				.find()
				.type(TwigKrcmickaEvaluation.class)
				.addFilter("pivar", FilterOperator.EQUAL,
						dataStore.associatedKey(beerer))
				.addFilter("krcmicka", FilterOperator.EQUAL,
						dataStore.associatedKey(pub)).returnResultsNow();
		List<TwigKrcmickaEvaluation> list = new ArrayList<TwigKrcmickaEvaluation>();

		while (iterator.hasNext()) {
			list.add(iterator.next());
		}
		return list;
	}

	@POST
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Path("/krcma")
	public void addPub(@FormParam("name") String name) {
		TwigKrcmicka krcmicka = new TwigKrcmicka();
		krcmicka.setName(name);

		Key store = dataStore.store(krcmicka);
	}

	@POST
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	@Path("/mock")
	public void createMocks() {
		for (TwigKrcmicka pub : Mocks.createMockPubs()) {
			Key store = dataStore.store(pub);
		}
		for (TwigPivar user : Mocks.createMockUsers()) {
			Key store = dataStore.store(user);
		}
		for (TwigEvaluationType evalType : Mocks.createMockEvalTypes()) {
			Key store = dataStore.store(evalType);
		}
	}

}
